//
//  LXPageTitleView.swift
//  zhibo
//
//  Created by Echo on 16/9/21.
//  Copyright © 2016年 Echo. All rights reserved.
//

import UIKit

// MARK:- 定义协议
protocol PageTitleViewDelegate : class {
    func pageTitleViewSelected(titleView : LXPageTitleView , selectedIndex index : Int)
}

// MARK:- 定义常量
private let kScrollLineH : CGFloat = 2
private let kNormalColor : (CGFloat , CGFloat ,CGFloat) = (85,85,85)
private let kSelectColor : (CGFloat , CGFloat , CGFloat) = (255,128,0)


class LXPageTitleView: UIView {
    
    // MARK:- 定义属性
    /// 标题数组
    private var titles : [String]
    /// pageView代理
    weak var delegate : PageTitleViewDelegate?
    /// 当前序列号
    private var currentIndex : Int = 0

    // MARK:- 懒加载属性
    /// 标题Label数组
    private lazy var titleLabels : [UILabel] = [UILabel]()
    /// 标题滚动条
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.scrollsToTop = false
        return scrollView
    }()
    /// 滑动条
    private lazy var scrollLine : UIView = {
        let scrollLine = UIView()
        scrollLine.backgroundColor = UIColor.orangeColor()
        return scrollLine
    }()
    
    // MARK:- 自定义构造方法
    init(frame: CGRect, titles : [String]) {
        self.titles = titles
        
        super.init(frame: frame)
        
        // 初始化界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK:- 初始化界面操作
extension LXPageTitleView {
    private func setupUI(){
        
        // 添加滚动条
        addSubview(scrollView)
        scrollView.frame = bounds
        
        // 添加标题label
        addTitleLabels()
        
        // 添加底边和滑动条
        addBottomLineAndScrollViewLine()
    }
    
    private func addTitleLabels(){
        
        let LabelW : CGFloat = frame.width / CGFloat(titles.count)
        let LabelH : CGFloat = frame.height - kScrollLineH
        let LabelY : CGFloat = 0
        
        for (index , title) in titles.enumerate() {
            let label = UILabel()
            
            label.text = title
            label.tag = index
            label.font = UIFont.systemFontOfSize(16.0)
            label.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
            label.textAlignment = .Center
            
            let LabelX : CGFloat = CGFloat(index) * LabelW
            label.frame = CGRectMake(LabelX, LabelY, LabelW, LabelH)
            
            scrollView.addSubview(label)
            titleLabels.append(label)
            
            // 添加手势
            label.userInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(_:)))
            label.addGestureRecognizer(tap)
        }
    }
    
    private func addBottomLineAndScrollViewLine(){
        
        // 添加底边
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lightGrayColor()
        
        let lineH : CGFloat = 0.5
        bottomLine.frame = CGRectMake(0, frame.height - lineH, frame.width, lineH)
        
        addSubview(bottomLine)
        
        // 给点击的label文字改变颜色，计算底部滚动条的尺寸
        guard let firstLabel = titleLabels.first else {return}
        firstLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: firstLabel.frame.origin.x, y: frame.height - kScrollLineH, width: firstLabel.frame.width, height: kScrollLineH)
    }
}

// MARK:- Objc 方法
extension LXPageTitleView {
    
     @objc private func titleLabelClick(tap : UITapGestureRecognizer){
        
        // 取出LABEL
        guard let currentLabel = tap.view as? UILabel else {
            return
        }
        
        // 判断是否重复点击
//        if currentLabel.tag == currentIndex { return }
        
        // 获取之前label
        let oldLabel = titleLabels[currentIndex]

        // 之前Label的颜色
        oldLabel.textColor = UIColor(r: kNormalColor.0, g: kNormalColor.1, b: kNormalColor.2)
        
        // 给最新点击的label改变状态，重新赋值
        currentLabel.textColor = UIColor(r: kSelectColor.0, g: kSelectColor.1, b: kSelectColor.2)
        currentIndex = currentLabel.tag
        
        // 滚动条位置改变
        let scrollLineX = CGFloat(currentIndex) * scrollLine.frame.width
        UIView.animateWithDuration(0.15) { 
            self.scrollLine.frame.origin.x = scrollLineX
        }
        
        // 通知代理
        delegate?.pageTitleViewSelected(self, selectedIndex: currentIndex)
    }
}

extension LXPageTitleView {
    func setTitleWithProgress(progress : CGFloat , sourceIndex : Int , targetIndex : Int){
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveRealX = moveTotalX * progress
        scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveRealX
        
        let deltaColor = (kSelectColor.0 - kNormalColor.0,kSelectColor.1 - kNormalColor.1,kSelectColor.2 - kNormalColor.2)
        
        sourceLabel.textColor = UIColor(r: kSelectColor.0 - deltaColor.0 * progress, g: kSelectColor.1 - deltaColor.1 * progress, b: kSelectColor.2 - deltaColor.2 * progress)
        targetLabel.textColor = UIColor(r: kNormalColor.0 + deltaColor.0 * progress, g: kNormalColor.1 + deltaColor.1 * progress, b: kNormalColor.2 + deltaColor.2 * progress)
        
        currentIndex = targetIndex
        
    }
}
